package com.hl.qzzpbackend.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hl.qzzpbackend.annotation.AuthCheck;
import com.hl.qzzpbackend.common.BaseResponse;
import com.hl.qzzpbackend.common.DeleteRequest;
import com.hl.qzzpbackend.common.ResultUtils;
import com.hl.qzzpbackend.model.dto.user.*;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.enums.UserRoleEnum;
import com.hl.qzzpbackend.model.vo.UserVO;
import com.hl.qzzpbackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 用户接口
 *  
 * @author helong
 */
@Slf4j
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;



    // 获取邮箱验证码
    @ApiOperation(value = "获取邮箱验证码")
    @RequestMapping(value = "/email/code", method = RequestMethod.POST)
    public BaseResponse<Boolean> userGetEmailCode(@Valid @RequestBody UserGetEmailCodeRequest userGetEmailCodeRequest) {
        if(log.isInfoEnabled()){
            log.info("获取邮箱验证码参数 {}", userGetEmailCodeRequest);
        }
        boolean result = userService.userGetEmailCode(userGetEmailCodeRequest);
        return ResultUtils.success(result);
    }

    // 用户账号登录
    @ApiOperation(value = "用户账号登录")
    @RequestMapping(value = "/login/account", method = RequestMethod.POST)
    public BaseResponse<UserVO> userAccountLogin(@Valid @RequestBody UserAccountLoginRequest userAccountLoginRequest,HttpServletRequest request) {
        if(log.isInfoEnabled()){
            log.info("用户账号登录参数 {}", userAccountLoginRequest);
        }
        UserVO userVO = userService.userAccountLogin(userAccountLoginRequest,request);
        return ResultUtils.success(userVO);
    }

    // 用户邮箱验证码登录
    @ApiOperation(value = "用户邮箱验证码登录")
    @RequestMapping(value = "/login/email", method = RequestMethod.POST)
    public BaseResponse<UserVO> userEmailLogin(@Valid @RequestBody UserEmailLoginRequest userEmailLoginRequest,HttpServletRequest request) {
        if(log.isInfoEnabled()){
            log.info("用户邮箱验证码登录 {}", userEmailLoginRequest);
        }
        UserVO userVO = userService.userEmailLogin(userEmailLoginRequest,request);
        return ResultUtils.success(userVO);
    }

    // 修改用户密码
    @ApiOperation(value = "修改用户密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public BaseResponse<Boolean> updatePassword(@Valid @RequestBody UserUpdatePasswordRequest userUpdatePasswordRequest) {
        if(log.isInfoEnabled()){
            log.info("修改用户密码 {}", userUpdatePasswordRequest);
        }
        boolean result = userService.updatePassword(userUpdatePasswordRequest);
        return ResultUtils.success(result);
    }

    // 重置用户密码
    @AuthCheck(mustRole = {UserRoleEnum.ADMIN,UserRoleEnum.SUPER_ADMIN})
    @ApiOperation(value = "重置用户密码 - 管理员接口")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.GET)
    public BaseResponse<Boolean> resetPassword(@Valid @RequestBody UserResetPasswordRequest userResetPasswordRequest) {
        if(log.isInfoEnabled()){
            log.info("重置用户密码 {}", userResetPasswordRequest);
        }
        boolean result = userService.resetPassword(userResetPasswordRequest);
        return ResultUtils.success(result);
    }

    // 用户登出
    @ApiOperation(value = "用户登出")
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        if(log.isInfoEnabled()){
            log.info("用户登出");
        }
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }


    // 用户注册
    @ApiOperation(value = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public BaseResponse<Boolean> userRegister(@Valid @RequestBody UserRegisterRequest userRegisterRequest) {
        if(log.isInfoEnabled()){
            log.info("用户注册参数 {}", userRegisterRequest);
        }
        boolean result = userService.userRegister(userRegisterRequest);
        return ResultUtils.success(result);
    }

    // 添加用户
    @AuthCheck(mustRole = {UserRoleEnum.ADMIN,UserRoleEnum.SUPER_ADMIN})
    @ApiOperation(value = "添加用户 - 管理员接口")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResponse<Boolean> userAdd(@Valid @RequestBody UserAddRequest userAddRequest) {
        if(log.isInfoEnabled()){
            log.info("添加用户 {}", userAddRequest);
        }
        boolean result = userService.userAdd(userAddRequest);
        return ResultUtils.success(result);
    }


    // 编辑用户信息
    @ApiOperation(value = "编辑用户信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public BaseResponse<Boolean> userEdit(@Valid @RequestBody UserEditRequest userEditRequest) {
        if(log.isInfoEnabled()){
            log.info("编辑用户信息参数 {}", userEditRequest);
        }
        boolean result = userService.userEdit(userEditRequest);
        return ResultUtils.success(result);
    }

    // 删除用户
    @AuthCheck(mustRole = {UserRoleEnum.ADMIN,UserRoleEnum.SUPER_ADMIN})
    @ApiOperation(value = "删除用户 - 管理员接口")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResponse<Boolean> userDelete(@Valid @RequestBody DeleteRequest deleteRequest) {
        if(log.isInfoEnabled()){
            log.info("删除用户参数 {}", deleteRequest);
        }
        boolean result = userService.userDelete(deleteRequest);
        return ResultUtils.success(result);
    }


    // 获取分页查询用户列表
    @AuthCheck(mustRole = {UserRoleEnum.ADMIN,UserRoleEnum.SUPER_ADMIN})
    @ApiOperation(value = "获取分页查询用户列表 - 管理员接口")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public BaseResponse<Page<UserVO>> getUserPage(@RequestBody UserQueryRequest userQueryRequest) {
        if(log.isInfoEnabled()){
            log.info("获取分页查询用户列表 {}", userQueryRequest);
        }
        Page<UserVO> userVOPage = userService.getUserPage(userQueryRequest);
        return ResultUtils.success(userVOPage);
    }











}
